﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WGLGridView;
using DBAccessor.Accessor;
using AlanUtilities;
using DBList.Entities;
using DBList;
using System.Collections.Generic;

public partial class SendGoodsTJ : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		String strUID = OperateControl.PickupAvaiableUserID(this);
		if (!IsPostBack)
		{
			int nSearchType = 1;
			strUID += '#';
			strUID += nSearchType.ToString();
			operaterLabel.Text = strUID;

			BindGrideView(nSearchType, DateTime.Now, DateTime.Now);
		}
	}
	private void ShowEmptyRecord()
	{
		string[] fields = { "Name","SingleMoney", "Number", "TotalMoney" };
		WGLGridView.WGLGridView.ClearAndShowEmptyGridView(GridView1, fields);
	}
	private bool PickupParameters(ref int outuserid, ref int outtype, ref DateTime outbdt, ref DateTime outedt)
	{
		bool bSuc = false;
		string strText = operaterLabel.Text;
		char[] pSplit = new char[1];
		pSplit[0] = '#';
		string[] listParam = strText.Split(pSplit);
		if (listParam.Length >= 2)
		{
			outuserid = 0;
			int.TryParse(listParam[0], out outuserid);
			outtype = 0;
			int.TryParse(listParam[1], out outtype);

			outbdt = DateTime.Now;
			outedt = DateTime.Now;
			if (listParam.Length >= 4)
			{
				DateTime.TryParse(listParam[2], out outbdt);
				DateTime.TryParse(listParam[3], out outedt);
			}
			else if (listParam.Length == 3)
			{
				DateTime.TryParse(listParam[2], out outbdt);
			}
			bSuc = true;
		}
		return bSuc;
	}
	protected void AspNetPager1_PageChanged(object sender, EventArgs e)
	{
		int nUID = 0, nType = 0;
		DateTime bdt = DateTime.Now, edt = DateTime.Now;
		if (PickupParameters(ref nUID, ref nType, ref bdt, ref edt))
		{
			BindGrideView(nType, bdt, edt);
		}
	}
	protected void BindGrideView(int nSearchType, DateTime srcBdt, DateTime srcEdt)
	{
		int nUID = 0, nType = 0;
		DateTime bdt = DateTime.Now, edt = DateTime.Now;
		if (!PickupParameters(ref nUID, ref nType, ref bdt, ref edt))
			return;
		int noutTotal = 0;
		List<int> idList = new List<int>();
		idList.Add(nUID);
		string strXmlParam = OperateControl.MakeXMLParamByList(idList);
		if (nType != nSearchType) nType = nSearchType;
		if (srcBdt != bdt) bdt = srcBdt;
		if (srcEdt != edt) edt = srcEdt;

		MsgAccessor msgAcce = new MsgAccessor();
		List<MDTJData> listData = msgAcce.MDKFWLTJGoodsKind(strXmlParam, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, nType, 4, ref bdt, ref edt, out noutTotal);
		GridView1.DataSource = listData;
		AspNetPager1.RecordCount = noutTotal;
		if (listData.Count == 0)
		{
			ShowEmptyRecord();
		}
		else
		{
			GridView1.DataBind();
		}
		string strInfo = nUID.ToString();
		strInfo += '#';
		strInfo += nType.ToString();
		recLabel.Text = "";
		if (nType == 3)
		{
			recLabel.Text = @"时间段，总记录：";
			recLabel.Text += noutTotal.ToString();
			recLabel.Text += @" 条";

			strInfo += '#';
			strInfo += bdt.ToString();
			strInfo += '#';
			strInfo += edt.ToString();
		}
		else if (nType == 1)
		{
			recLabel.Text = @"今天，总记录：";
			recLabel.Text += noutTotal.ToString();
			recLabel.Text += @" 条";
		}
		else if (nType == 2)
		{
			recLabel.Text = @"当月，总记录：";
			recLabel.Text += noutTotal.ToString();
			recLabel.Text += @" 条";
		}
		operaterLabel.Text = strInfo;
	}
	protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onclick", "GridView_selectRow(this)");
			e.Row.Height = 25;
		}
		else if (e.Row.RowType == DataControlRowType.Header)
		{
			e.Row.Height = 25;
		}
	}
	protected void btnToday_Click(object sender, EventArgs e)
	{
		tbBeginTime.Text = "";
		tbEndTime.Text = "";
		AspNetPager1.CurrentPageIndex = 1;
		BindGrideView(1, DateTime.Now, DateTime.Now);
	}
	protected void btnMonth_Click(object sender, EventArgs e)
	{
		tbBeginTime.Text = "";
		tbEndTime.Text = "";
		AspNetPager1.CurrentPageIndex = 1;
		BindGrideView(2, DateTime.Now, DateTime.Now);
	}
	protected void btnSearch_Click(object sender, EventArgs e)
	{
		DateTime bdt, edt;
		string strBegin, strEnd;
		strBegin = tbBeginTime.Text.Trim();
		strEnd = tbEndTime.Text.Trim();
		if (DateTime.TryParse(strBegin, out bdt))
		{
			if (strEnd.Length == 0)
			{
				edt = DateTime.Now;
			}
			else if (DateTime.TryParse(strEnd, out edt))
			{
				strEnd = edt.ToShortDateString();
				strEnd += @" 23:59:59";
				DateTime.TryParse(strEnd, out edt);
			}
			else
			{
				return;
			}
			AspNetPager1.CurrentPageIndex = 1;
			BindGrideView(3, bdt, edt);
		}
		else if (strBegin.Length == 0 && DateTime.TryParse(strEnd, out edt))
		{
			strEnd = edt.ToShortDateString();
			strEnd += @" 23:59:59";
			DateTime.TryParse(strEnd, out edt);
			strBegin = edt.Year.ToString();
			strBegin += "-01-01 00:00:01";
			DateTime.TryParse(strBegin, out bdt);

			AspNetPager1.CurrentPageIndex = 1;
			BindGrideView(3, bdt, edt);
		}
		else if (GridView1.IsEmptyShowing())
		{
			ShowEmptyRecord();
		}
	}
}
